package com.viphuli.http.handler;

import java.util.List;

import org.apache.http.Header;

import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.offroader.utils.JsonUtils;
import com.offroader.utils.LogUtils;
import com.viphuli.app.R;
import com.viphuli.common.Constants;
import com.viphuli.fragment.HomeFragment;
import com.viphuli.http.bean.page.HomePage;
import com.viphuli.http.bean.part.HomeCompanyOrder;
import com.viphuli.http.bean.part.HomePrivateCustom;
import com.viphuli.http.bean.part.HomeService;
import com.viphuli.http.bean.part.HomeTopSlide;
import com.viphuli.util.ImageUtils;

public class HomeResponseHandler extends MyBaseHttpResponseHandler<HomeFragment, HomePage> {
	public static final String SLIDE_KEY = "slide_Key";

	@Override
	public void deal() {

		//顶部轮转图
		List<HomeTopSlide> topSlides = page.getTopSlide();

		if (topSlides != null && topSlides.size() == 1) {
			caller.getSlider().stopAutoCycle();
		}else{
			caller.getSlider().setDuration(Constants.TOP_SLIDE_DURATION);
		}
		
		caller.getSlider().removeAllSliders();

		for (HomeTopSlide topSlide : topSlides) {
			
			TextSliderView textSliderView = new TextSliderView(caller.getActivity());

			textSliderView.//
					description(topSlide.getTitle()).//标题
					image(topSlide.getPic()).//图片
					setScaleType(BaseSliderView.ScaleType.Fit).//
					setOnSliderClickListener(caller);

			//add your extra information
			textSliderView.getBundle().putString(SLIDE_KEY, JsonUtils.toJson(topSlide));
			
			caller.getSlider().addSlider(textSliderView);
			
		}

		//上门体检&上门护理
		HomeService homeService = null;
		for (int i = 0; i < page.getServiceList().size(); i++) {
			homeService = page.getServiceList().get(i);

			if (i == 0) {

				ImageUtils.load(R.drawable.ic_main_content_health, caller.getHealthIcon(), homeService.getIcon());
				caller.getHealthTitle().setText(homeService.getName());
				caller.getHealthContent().setText(homeService.getDesc());

			} else if (i == 1) {

				ImageUtils.load(R.drawable.ic_main_content_nurse, caller.getNurseIcon(), homeService.getIcon());
				caller.getNurseTitle().setText(homeService.getName());
				caller.getNurseContent().setText(homeService.getDesc());

			}

		}

		//企业定制
		HomeCompanyOrder homeCompanyOrder = page.getCompanyOrder();
		ImageUtils.load(R.drawable.ic_main_content_company_order, caller.getCompanyIcon(), homeCompanyOrder.getIcon());
		caller.getCompanyTitle().setText(homeCompanyOrder.getName());

		//私人定制
		HomePrivateCustom homePrivateCustom = page.getPrivateCustom();
		ImageUtils.load(R.drawable.ic_main_content_private_custom, caller.getPrivateIcon(), homePrivateCustom.getIcon());
		caller.getPrivateTitle().setText(homePrivateCustom.getName());

	}

	@Override
	public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, HomePage home) {
		super.onSuccess(statusCode, headers, rawJsonResponse, home);

		if (!caller.isAdded() || home == null)
			return;

	}

	@Override
	public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonResponse, HomePage home) {
		super.onFailure(statusCode, headers, throwable, rawJsonResponse, home);
		LogUtils.info("@@@@@@@@@@@@@@@@@onFailure@" + statusCode+"|"+caller.isAdded());
		
		if (!caller.isAdded())
			return;
		

	}

}
